
//
// 110216a- AH - (Bien) Created.
// 110216b- AH - (Garage) Clear.
// 110305a- AH - (Fantoft) getInput().
// 110205b- AH - (Fantoft) Made iterable over Line.
// 110310a- AH - add(String).
// 110310b- AH - Lines(String). 
// 110313 - AH - Lines(Collection<String>) and add(Collection<String>).
// 110517 - AH - toFlatString().
// 110607 - AH - Removed not used method toFlatString() and not used 
//               constructor.
// 120321 - AH - Removed @Override for interface method (1.5 compatible).
// 120409 - AH - empty().
//

package org.soekrat.engine.visitor;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.soekrat.AttributeObject;


/**
 * <p>
 *   Lines from visitor input.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class Lines extends AttributeObject implements Iterable<Line> 
{
  private final List<Line> m_lines=new ArrayList<Line>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Lines(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param l Line to be added to created.
   */
  public Lines(String l){ add(l); }
  
  /**
   * <p>
   *   Adds
   *   {@link Line}.
   * </p>
   * @param l {@code Line} to add.
   * @return {@code this}.
   */
  public Lines add(String l)
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    l=l.trim();
    if (l.length()>0) add(new Line(l));
    return this;
  }
  
  /**
   * <p>
   *   Adds
   *   {@link Line}.
   * </p>
   * @param l {@code Line} to add.
   * @return {@code this}.
   */
  public Lines add(Line l)
  {
    if (l==null)
    {
      throw new NullPointerException("l");
    }
    
    m_lines.add(l);
    return this;
  }
  
  /**
   * <p>
   *   Adds 
   *   {@link Line}s created from given
   *   {@link String}s.
   * </p>
   * @param lines {@link String}s.
   * @return {@code this}.
   */
  public Lines add(Collection<String> lines)
  {
    if (lines==null)
    {
      throw new NullPointerException("lines");
    }
    
    int i=0;
    for (String curr : lines)
    {
      if (curr==null)
      {
        throw new NullPointerException("line "+i);  
      }
      
      add(curr);
      
      i++;
    }
    
    return this;
  }
  
  /**
   * <p>
   *   Gets the number of lines.
   * </p>
   * @return Count.
   */
  public int size(){ return m_lines.size(); }
  
  /**
   * <p>
   *   Tells if this has no lines.
   * </p>
   * @return {@link #size()}{@code ==0}.
   */
  public boolean empty(){ return size()==0; }
  
  /**
   * <p>
   *   Gets 
   *   {@link Line}.
   * </p>
   * @param idx Index of
   *            {@code Line} to get.
   * @return {@code Line}.
   * @throws IndexOutOfBoundsException If {@code idx<0 || idx>size()-1}.
   */
  public Line get(int idx){ return m_lines.get(idx); }
  
  /**
   * <p>
   *   Clears all lines.
   * </p>
   */
  public void clear(){ m_lines.clear(); }
  
  /**
   * <p>
   *   Gets input to Soekrat from visitor input. 
   * </p>
   * @return {@link TokenizedLine}.
   */
  public TokenizedLine getInput()
  {
    TokenizedLine retVal=new TokenizedLine();
    
    for (Line line : m_lines)
    {
      line.getInput(retVal);  
    }
    
    return retVal;
  }
  
  @Override
  public String toString() 
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    for (Line line : m_lines) pw.println(line);
      
    pw.flush();
    return caw.toString();
  }

  // Declared Iterable<Line>.
  public Iterator<Line> iterator(){ return m_lines.iterator(); }
  
}
